﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using ScrapySharp.Extensions;

namespace NC.Cather
{
    public class CatherManager
    {
        /// <summary>
        /// 获取UrlList的地址列表
        /// </summary>
        /// <param name="urlList"></param>
        /// <returns></returns>
        public static List<LinkResult> GetUrlList(UrlList urlList)
        {
            List<LinkResult> linkResults = new List<LinkResult>();
            //开始解析Html
            HtmlWeb web = new HtmlWeb();
            web.OverrideEncoding = Encoding.UTF8;
            //下载目标Html页面代码
            HtmlDocument homeDocument = web.Load(urlList.Url);
            //获取List列表Content
            var listContainer = homeDocument.DocumentNode.CssSelect(urlList.ContentSelector).FirstOrDefault();
            listContainer = listContainer.RemoveStylesAndScripts();
            var aLis = listContainer.CssSelect(urlList.ASelector);

            //遍历获取里面的值
            LinkResult linkResult;
            foreach (var list in aLis)
            {
                linkResult = new LinkResult();
                linkResult.Url = list.GetAttributeValue("href");
                linkResult.Text = list.InnerText;
                linkResults.Add(linkResult);
            }
            return linkResults;
        }
        public static List<NewsTransferEntity> GetNews(List<LinkResult> linkResults, ContentParams contentParams, List<ContentExtendParams> contentExtendParams)
        {
            List<NewsTransferEntity> newsTransferEntities = new List<NewsTransferEntity>();
            NewsTransferEntity newsTransferEntity;

            //Web 工具
            //开始解析Html
            HtmlWeb web = new HtmlWeb();
            web.OverrideEncoding = Encoding.UTF8;
            foreach (var linkResult in linkResults)
            {
                newsTransferEntity = new NewsTransferEntity();
                //下载目标Html页面代码
                HtmlDocument homeDocument = web.Load(linkResult.Url);

                //开始基础文章字段内容的筛选
                //获取Container
                var containerDocument =
                    homeDocument.DocumentNode.CssSelect(contentParams.ContainerSelector).FirstOrDefault();
                //删除内部标签
                containerDocument = containerDocument.RemoveStylesAndScripts();
                //获取Title
                newsTransferEntity.Title =
                    containerDocument.CssSelect(contentParams.TitleSelector).FirstOrDefault().InnerText;

                //获取Author
                newsTransferEntity.Author =
                    containerDocument.CssSelect(contentParams.AuthorSelector).FirstOrDefault().InnerText;

                //获取发布日期
                newsTransferEntity.ComeUpTime =
                    containerDocument.CssSelect(contentParams.ComeUpTimeSelector).FirstOrDefault().InnerText;

                //获取文章的相信内容
                newsTransferEntity.Content =
                    containerDocument.CssSelect(contentParams.ContentSelector).FirstOrDefault().InnerHtml;

                //开始获取文章的扩展字段
                Dictionary<string, string> extendedFields = new Dictionary<string, string>();
                foreach (var contentExtendParam in contentExtendParams)
                {
                    string value = "";
                    if (contentExtendParam.InfoType == InfoType.Html)
                        value = containerDocument.CssSelect(contentExtendParam.Selector).FirstOrDefault().OuterHtml;
                    else
                        value = containerDocument.CssSelect(contentExtendParam.Selector).FirstOrDefault().InnerText;
                    extendedFields.Add(contentExtendParam.Key, value);
                }
                newsTransferEntity.KeyValues = extendedFields;

                newsTransferEntities.Add(newsTransferEntity);
            }
            return newsTransferEntities;
        }
        /// <summary>
        /// 获取指定所有DOM节点
        /// </summary>
        /// <param name="url">网站地址</param>
        /// <param name="selector">样式选择器</param>
        /// <param name="method">httpMethod</param>
        /// <returns>返回结果HtmlNode数组</returns>
        public static List<HtmlNode> GetNodes(string url, string selector, HttpMethod method)
        {
            List<LinkResult> linkResults = new List<LinkResult>();
            //开始解析Html
            HtmlWeb web = new HtmlWeb();
            web.OverrideEncoding = Encoding.UTF8;
            //下载目标Html页面代码
            HtmlDocument homeDocument = web.Load(url,method.Method);
            //获取List列表Content
            var listContainer = homeDocument.DocumentNode.CssSelects(selector).ToList();
            foreach (var container in listContainer)
            {
                container.RemoveStylesAndScripts();
            }
            return listContainer;
        }
        /// <summary>
        /// 获取两级样式选择器
        /// </summary>
        /// <param name="url">网站地址</param>
        /// <param name="firstSelector">一级样式选择器</param>
        /// <param name="secondSelector">二级样式选择器</param>
        /// <param name="method">HttpMethod</param>
        /// <returns>返回最终HtmlNode</returns>
        public static List<HtmlNode> GetNodes(string url, string firstSelector, string secondSelector, HttpMethod method)
        {
            var firstNodes = GetNodes(url, firstSelector, method);
            List<HtmlNode> resultNodes=new List<HtmlNode>();
            foreach (var firstNode in firstNodes)
            {
                resultNodes.AddRange(firstNode.CssSelects(secondSelector));
            }
            return resultNodes;
        }
        /// <summary>
        /// 获取指定所有DOM Html代码
        /// </summary>
        /// <param name="url">网站地址</param>
        /// <param name="selector">样式选择器</param>
        /// <param name="method">httpMethod</param>
        /// <returns>返回结果Html</returns>
        public static string GetHtml(string url,string selector,HttpMethod method)
        {
            var listContainer = GetNodes(url, selector, method);
            string innerHtml="";
            foreach (var node in listContainer)
            {
                innerHtml += node.OuterHtml;
            }
            return innerHtml;
        }
    }
}
